<template>
    <div>
        <!--        面包屑导航栏-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!--        卡片视图-->
        <el-card>
            <!--            搜索框与按钮-->
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="6">
                    <el-button type="primary" @click="toAddPage">添加商品</el-button>
                </el-col>
            </el-row>
            <!--            table表格-->
            <el-table :data="goodsList" stripe border>
                <el-table-column type="index" label="#" align="center"></el-table-column>
                <el-table-column prop="goods_name" label="商品名称" width="500px" align="center"></el-table-column>
                <el-table-column prop="goods_price" label="商品价格(元)" width="120px" align="center"></el-table-column>
                <el-table-column prop="goods_weight" label="商品重量" width="100px" align="center"></el-table-column>
                <el-table-column prop="goods_number" label="商品数量" width="140px" align="center"></el-table-column>
                <el-table-column label="创建时间" width="180px" align="center">
                    <template slot-scope="scope">
                        {{scope.row.add_time | showDate}}
                    </template>
                </el-table-column>
                <el-table-column align="center" label="操作">
                    <template slot-scope="scope">
                        <el-button icon="el-icon-edit" type="primary"
                                   @click="showEditDialog(scope.row.goods_id)"></el-button>
                        <el-button icon="el-icon-delete" type="danger"
                                   @click="deleteGoods(scope.row.goods_id)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--            分页-->
            <el-pagination class="page"
                           @size-change="handleSizeChange"
                           @current-change="handleCurrentChange"
                           :current-page="queryInfo.pagenum"
                           :page-sizes="[5, 10, 15, 20]"
                           :page-size="queryInfo.pagesize"
                           background
                           layout="total, sizes, prev, pager, next, jumper"
                           :total="total">
            </el-pagination>
        </el-card>
        <!--        修改商品信息dialog-->
        <el-dialog title="修改商品信息" :visible.sync="editDialogVisible" width="40%" @close="editDialogClosed">
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
                <el-form-item label="商品名称">
                    <el-input disabled v-model="editForm.goods_name"></el-input>
                </el-form-item>
                <el-form-item label="商品价格" prop="goods_price">
                    <el-input type="number" v-model="editForm.goods_price"></el-input>
                </el-form-item>
                <el-form-item label="商品重量" prop="goods_weight">
                    <el-input type="number" v-model="editForm.goods_weight"></el-input>
                </el-form-item>
                <el-form-item label="商品数量" prop="goods_number">
                    <el-input type="number" v-model="editForm.goods_number"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editGoods">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {dateFormat} from "../../common/utils";

    export default {
        name: "List",
        data() {
            return {
                queryInfo: {
                    query: '',
                    pagenum: 1,
                    pagesize: 10
                },
                total: 0,
                goodsList: [],
                // 修改商品信息
                editDialogVisible: false,
                editForm: {},
                editFormRules: {
                    goods_price: [
                        {required: true, message: '请输入价格', trigger: 'blur'},
                    ],
                    goods_number: [
                        {required: true, message: '请输入数量', trigger: 'blur'},
                    ],
                    goods_weight: [
                        {required: true, message: '请输入重量', trigger: 'blur'},
                    ]
                }
            }
        },
        created() {
            this.getGoodsList()
        },
        methods: {
            async getGoodsList() {
                const res = (await this.$http.get('goods', {params: this.queryInfo})).data
                if (res.meta.status !== 200) {
                    return this.$message.error({
                        message: '获取商品列表失败!',
                        center: true
                    })
                } else {
                    this.goodsList = res.data.goods
                    this.total = res.data.total
                }
            },
            handleSizeChange(newSize) {
                this.queryInfo.pagesize = newSize
                this.getGoodsList()
            },
            handleCurrentChange(newPage) {
                this.queryInfo.pagenum = newPage
                this.getGoodsList()
            },
            async deleteGoods(id) {
                const info = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(err => err)
                if (info === "cancel") {
                    return this.$message.info({
                        message: '已取消删除!',
                        center: true
                    })
                } else {
                    const res = (await this.$http.delete('goods/' + id)).data
                    if (res.meta.status != 200) {
                        return this.$message.error({
                            message: '删除失败!',
                            center: true
                        })
                    } else {
                        this.$message.success({
                            message: '删除成功!',
                            center: true
                        })
                        await this.getGoodsList()
                    }
                }
            },
            toAddPage() {
                this.$router.push('/goods/add')
            },
            async showEditDialog(id) {
                // // 如果这里传入的参数是scope.row会有问题,具体为什么还不是很懂
                this.editDialogVisible = true
                const res = (await this.$http.get('goods/' + id)).data
                if (res.meta.status !== 200) {
                    return this.$message.error({
                        message: '查询商品信息失败！',
                        center: true
                    })
                } else {
                    this.editForm = res.data
                }

            },
            editDialogClosed() {
                this.$refs.editFormRef.resetFields()

            },
            editGoods() {
                this.$refs.editFormRef.validate(async valid => {
                    if (!valid) {
                        return
                    } else {
                        const res = (await this.$http.put('goods/' + this.editForm.goods_id, this.editForm)).data
                        if (res.meta.status !== 200) {
                            return this.$message.error({
                                message: '修改商品信息失败！',
                                center: true
                            })
                        } else {
                            this.$message.success({
                                message: '修改商品信息成功！',
                                center: true
                            })
                            await this.getGoodsList()
                            this.editDialogVisible = false
                        }
                    }
                })
            }
        },
        filters: {
            showDate(value) {
                return dateFormat(value)
            }
        }
    }
</script>

<style scoped>
    .page {
        width: 700px;
        margin: 15px auto;
    }
</style>